//
//  SystemSecondNotiController.m
//  coba-iOS
//
//  Created by 郭四虎 on 15/12/8.
//  Copyright © 2015年 YouKu. All rights reserved.
//

#import "SystemSecondNotiController.h"

@interface SystemSecondNotiController ()
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UILabel *headerNamelb;
@property (weak, nonatomic) IBOutlet UILabel *headerDatelb;
@property (weak, nonatomic) IBOutlet UILabel *headerContentlb;
@property (weak, nonatomic) IBOutlet UIImageView *headerIconIma;

@property (weak, nonatomic) IBOutlet UIView *replyView;
@property (weak, nonatomic) IBOutlet UIImageView *replyIma;
@property (weak, nonatomic) IBOutlet UILabel *replyNamelb;
@property (weak, nonatomic) IBOutlet UILabel *replyDate;
@property (weak, nonatomic) IBOutlet UILabel *replyContent;

@property (weak, nonatomic) IBOutlet UIView *myReplyView;
@property (weak, nonatomic) IBOutlet UIImageView *myReplyIcon;
@property (weak, nonatomic) IBOutlet UILabel *myReplyname;
@property (weak, nonatomic) IBOutlet UILabel *myReplyDate;
@property (weak, nonatomic) IBOutlet UILabel *myReplycontent;


@property (weak, nonatomic) IBOutlet UITextField *replyTf;
@property (weak, nonatomic) IBOutlet UIView *tfView;

@end

@implementation SystemSecondNotiController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    [self initUI];
}

- (void)keyboardWillShow:(NSNotification *)aNotification
{
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int height = keyboardRect.size.height;
    //    int width = keyboardRect.size.width;
    //    NSLog(@"键盘高度是  %d",height);
    //    NSLog(@"键盘宽度是  %d",width);
    
    CGRect rect = _tfView.frame;
    rect.origin.y = HEIGHT-50*SCALEY - height-64;
    _tfView.frame = rect;
}

- (void)keyboardWillHide:(NSNotification *)aNotification
{
    CGRect rect = _tfView.frame;
    rect.origin.y = HEIGHT-50*SCALEY-64;
    _tfView.frame = rect;
}

- (void)initUI
{
    _myReplyView.hidden = YES;
    
    _headerContentlb.text = _model.content;
    _headerDatelb.text = [MyDateformater formater:_model.reply_date];
    //_headerNamelb.text = _model.user_name;
    
    _replyNamelb.text = _model.from_user_name;
    _replyDate.text = [MyDateformater formater:_model.from_reply_date];
    
    NSString *parentStr = [NSString stringWithFormat:@"回复我:%@",_model.from_content];
    NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:parentStr];
//    [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor]range:NSMakeRange(0, 3+_model.user_name.length)];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor]range:NSMakeRange(0, 4)];
    
    _replyContent.attributedText = str;
    
    [_replyTf becomeFirstResponder];
}
- (IBAction)replyClick:(id)sender {
    
    if (!_replyTf.text.length) {
        [self.view Message:@"请输入回复内容" HiddenAfterDelay:1];
        return;
    }
    NSLog(@"%@+++,%@",_model.from_parent_ids, _model.from_id);
    if (_model.from_parent_ids) {
        _model.from_parent_ids = [NSString stringWithFormat:@"%@,%@", _model.from_id,_model.from_parent_ids];
        NSLog(@"%@+++,%@",_model.from_parent_ids, _model.from_id);
    }else{
        _model.from_parent_ids = _model.from_id;
    }
    NSLog(@"%@+++,%@",_model.from_parent_ids, _model.from_id);
    
    NSDictionary *parameter = @{@"obj_id":_model.obj_id,@"type":_model.type,@"parent_id":_model.from_id,@"parent_ids":_model.from_parent_ids,@"user_id":[Myuser sharedUser].login_id,@"user_name":[Myuser sharedUser].name,@"content":_replyTf.text,@"reply_date":[MyDateformater getCurrentDate]};
    
    [MyAFN POST:EVALUATIONADDREPLY parameters:parameter block:^(id responseObject) {
       
        NSDictionary *response = responseObject;
        NSLog(@"%@",response);
        if ([[response objectForKey:@"status"]integerValue]) {
            
            [self.view Message:@"回复成功" HiddenAfterDelay:1];
            [self showMyreply];
            [self cleanTf];
        }
        
    }error:^(NSError *error) {
        
    }fromController:self];
}

- (void)cleanTf
{
    _replyTf.text = @"";
    [_replyTf resignFirstResponder];
}

- (void)showMyreply
{
    _myReplyView.hidden = NO;
    _myReplyDate.text = [MyDateformater formater:[MyDateformater getCurrentDate]];
    
    NSString *parentStr = [NSString stringWithFormat:@"我回复%@:%@",_model.from_user_name,_replyTf.text];
    NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:parentStr];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor]range:NSMakeRange(0, 4 + _model.from_user_name.length)];
    
    _myReplycontent.attributedText = str;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
